function waitForElementBySelector(selector) {
    return new Promise(resolve => {
        if (document.querySelector(selector)) {
            return resolve(document.querySelector(selector))
        }

        const observer = new MutationObserver(mutations => {
            if (document.querySelector(selector)) {
                resolve(document.querySelector(selector));
                observer.disconnect();
            }
        })

        observer.observe(document.body, {
            childList: true,
            subtree: true
        })
    })
}

let s = document.createElement('script')
s.src = chrome.runtime.getURL('jsInject.js')
s.onload = function() {
    this.remove()
};

if(window.location.href.includes('live_chat')) {
  (document.head || document.documentElement).appendChild(s)
  console.log('popout')
} else {
  console.log('normal')
  setTimeout(() => {
    waitForElementBySelector('#chatframe').then((element) => {
      element.contentDocument.head.appendChild(s)
      console.log(element)
    })
  }, 5000)
}